//
//  HDleftTitleTextField.m
//  AccountEasy
//
//  Created by 黄志刚 on 2020/8/13.
//  Copyright © 2020 AccountEasy. All rights reserved.
//

#import "HDleftTitleTextField.h"

@interface HDleftTitleTextField()<UITextFieldDelegate>

@property (nonatomic, assign) int count;
@property (nonatomic, strong) UIButton *validationBtn;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation HDleftTitleTextField

+(instancetype)initWithLeftTitle:(NSString *)title Placeholder:(NSString *)placeholder AndPoint:(CGPoint)point {
    HDleftTitleTextField *view = [[HDleftTitleTextField alloc] initWithFrame:SARectMake(point.x, point.y, 299, 31)];
    
    UILabel *leftLab = [[UILabel alloc] initWithFrame:SARectMake(0, 0, 94, 30)];
    leftLab.font = kFont(13);
    leftLab.textAlignment = NSTextAlignmentLeft;
    leftLab.textColor = kBlackColor;
    leftLab.text = title;
    [view addSubview:leftLab];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:SARectMake(95, 0, 204, 30)];
    view.textField = textField;
    textField.delegate = view;
    textField.textColor = kBlackColor;
    [view addSubview:textField];
    textField.font = kFont(13);
    textField.placeholder = placeholder;
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 30, 299, 1)];
    line.backgroundColor = kColor(217, 217, 217, 1);
    [view addSubview:line];
     
    return view;
}

+(instancetype)initWithSelectLeftTitle:(NSString *)title Placeholder:(NSString *)placeholder AndPoint:(CGPoint)point {
    HDleftTitleTextField *view = [[HDleftTitleTextField alloc] initWithFrame:SARectMake(point.x, point.y, 299, 31)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:view action:@selector(clickedTextField)];
    [view addGestureRecognizer:tap];
    
    UILabel *leftLab = [[UILabel alloc] initWithFrame:SARectMake(0, 0, 94, 30)];
    leftLab.font = kFont(13);
    leftLab.textAlignment = NSTextAlignmentLeft;
    leftLab.textColor = kBlackColor;
    leftLab.text = title;
    [view addSubview:leftLab];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:SARectMake(95, 0, 184, 30)];
    view.textField = textField;
    textField.textColor = kBlackColor;
    [view addSubview:textField];
    textField.font = kFont(13);
    textField.userInteractionEnabled = NO;
    textField.placeholder = placeholder;
    
    UIImageView *downImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blackDownAllow"]];
    downImageV.frame = SARectMake(280, 10, 19, 11);
    [view addSubview:downImageV];
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 30, 299, 1)];
    line.backgroundColor = kColor(217, 217, 217, 1);
    [view addSubview:line];
     
    return view;
}

+(instancetype)initWithValidationLeftTitle:(NSString *)title Placeholder:(NSString *)placeholder AndPoint:(CGPoint)point {
    HDleftTitleTextField *view = [[HDleftTitleTextField alloc] initWithFrame:SARectMake(point.x, point.y, 299, 31)];
    
    UILabel *leftLab = [[UILabel alloc] initWithFrame:SARectMake(0, 0, 94, 30)];
    leftLab.font = kFont(13);
    leftLab.textAlignment = NSTextAlignmentLeft;
    leftLab.textColor = kBlackColor;
    leftLab.text = title;
    [view addSubview:leftLab];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:SARectMake(95, 0, 134, 30)];
    view.textField = textField;
    textField.textColor = kBlackColor;
    [view addSubview:textField];
    textField.font = kFont(13);
    textField.placeholder = placeholder;
    
    UIButton *validationBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    validationBtn.frame = SARectMake(229, 0, 65, 28);
    [validationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    validationBtn.titleLabel.font = kBFont(12);
    [validationBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [validationBtn setBackgroundImage:[UIImage imageNamed:@"validation_btn"] forState:UIControlStateNormal];
    [validationBtn addTarget:view action:@selector(clickedValidationBtn:) forControlEvents:UIControlEventTouchUpInside];
    view.validationBtn = validationBtn;
    [view addSubview:validationBtn];
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 30, 299, 1)];
    line.backgroundColor = kColor(217, 217, 217, 1);
    [view addSubview:line];
     
    return view;
}

+(instancetype)initWithLeftTitle48:(NSString *)title Placeholder:(NSString *)placeholder AndPoint:(CGPoint)point {
    HDleftTitleTextField *view = [[HDleftTitleTextField alloc] initWithFrame:SARectMake(point.x, point.y, 299, 48)];
    
    UILabel *leftLab = [[UILabel alloc] initWithFrame:SARectMake(0, 25, 66, 14)];
    leftLab.font = kFont(13);
    leftLab.textAlignment = NSTextAlignmentLeft;
    leftLab.textColor = kBlackColor;
    leftLab.text = title;
    [view addSubview:leftLab];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:SARectMake(81, 25, 218, 14)];
    view.textField = textField;
    textField.delegate = view;
    textField.textColor = kBlackColor;
    [view addSubview:textField];
    textField.font = kFont(13);
    textField.placeholder = placeholder;
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 47, 299, 1)];
    line.backgroundColor = kColor(217, 217, 217, 1);
    [view addSubview:line];
     
    return view;
}

+(instancetype)initWithSelectLeftTitle48:(NSString *)title Placeholder:(NSString *)placeholder AndPoint:(CGPoint)point {
    HDleftTitleTextField *view = [[HDleftTitleTextField alloc] initWithFrame:SARectMake(point.x, point.y, 299, 48)];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:view action:@selector(clickedTextField)];
    [view addGestureRecognizer:tap];
    
    UILabel *leftLab = [[UILabel alloc] initWithFrame:SARectMake(0, 25, 66, 14)];
    leftLab.font = kFont(13);
    leftLab.textAlignment = NSTextAlignmentLeft;
    leftLab.textColor = kBlackColor;
    leftLab.text = title;
    [view addSubview:leftLab];
    
    UITextField *textField = [[UITextField alloc] initWithFrame:SARectMake(81, 25, 200, 14)];
    view.textField = textField;
    textField.textColor = kBlackColor;
    [view addSubview:textField];
    textField.font = kFont(13);
    textField.userInteractionEnabled = NO;
    textField.placeholder = placeholder;
    
    UIImageView *downImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blackDownAllow"]];
    downImageV.frame = SARectMake(267, 20, 19, 11);
    [view addSubview:downImageV];
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 47, 299, 1)];
    line.backgroundColor = kColor(217, 217, 217, 1);
    [view addSubview:line];
     
    return view;
}

-(void)clickedTextField {
    !self.clickedViewBlock ?: self.clickedViewBlock();
}

-(void)startTime {
    self.validationBtn.userInteractionEnabled = NO;
    [self.validationBtn setTitle:[NSString stringWithFormat:@"剩余60秒"] forState:UIControlStateNormal];
    self.count = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}

-(void)clickedValidationBtn:(UIButton *)sender {

    !self.clickedViewBlock ?: self.clickedViewBlock();
}

- (void)timerFired {
    if (self.count != 1) {
        self.count -= 1;
        [self.validationBtn setTitle:[NSString stringWithFormat:@"剩余%d秒", self.count] forState:UIControlStateNormal];
    } else {
        self.validationBtn.userInteractionEnabled = YES;
        [self.validationBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        // 停掉定时器
        [self.timer invalidate];
        self.timer = nil;
    }
}

@end
